package com.darrenchan.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-12-31 23:23
 */
public class Q953 {
    public boolean isAlienSorted(String[] words, String order) {
        int[] map = new int[26];
        for (int i = 0; i < order.length(); i++) {
            map[order.charAt(i) - 'a'] = i;
        }
        for (int i = 0; i < words.length - 1; i++) {
            String word1 = words[i];
            String word2 = words[i + 1];

            boolean isDiff = false;
            for (int j = 0; j < Math.min(word1.length(), word2.length()); j++) {
                if (word1.charAt(j) != word2.charAt(j)) {
                    isDiff = true;
                    if (map[word1.charAt(j) - 'a'] > map[word2.charAt(j) - 'a']) {
                        return false;
                    }
                    break;
                }
            }

            if(!isDiff){
                if (word1.length() > word2.length()) {
                    return false;
                }
            }
        }

        return true;
    }
}
